/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.search;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import linkeddata.arida.ufc.br.VCD.ElementsVCDS;

/**
 *
 * @author jeovane
 */
public class FileHierarchyVCDS {

    private ElementsVCDS elementsVCDS;
    private Graph graph = new Graph();
    private List<String> nameVCDDestination = new ArrayList<String>();
    private Set<String> setNodes = new LinkedHashSet<String>();

    public FileHierarchyVCDS(ElementsVCDS elementsVCDS) {
        this.elementsVCDS = elementsVCDS;
    }

    public Set<String> CorrectHierarchyVCDS() {
        // Adicionar o nome da VCD juntamente com Elos -> Destino
        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
            for (int j = 0; j < elementsVCDS.getExtractList().get(i).getElosVCD().size(); j++) {
                nameVCDDestination.add(elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getNameVCDExtract()
                        + "," + elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getDestinationVCD());

                // Adiciona o nome das VCDS aos nós da arvore
                Node node = new Node(elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getNameVCDExtract());
                graph.setNodes(node);
            }
        }

        // Adiciona as VCDS remetente -> destino aos nós da árvore
        for (int i = 0; i < graph.getNodes().size(); i++) {
            Node nodei = (Node) graph.getNodes().get(i);
            for (int j = 0; j < graph.getNodes().size(); j++) {
                Node nodej = (Node) graph.getNodes().get(j);
                if (nameVCDDestination.contains(nodei.label + "," + nodej.label)) {
                    graph.connectNode(nodei, nodej);
                }
            }
        }
        
        // Adiciona o nó pai ao Graph
        for (int i = 0; i < graph.getNodes().size(); i++) {
            Node node = (Node) graph.getNodes().get(i);
            if (node.label.contains(parentNode())) {
                graph.setRootNode(node);
            }
        }
        
        // Adiciona os nodes ao Set<>
        for (Node nodes : graph.bfs()){
            setNodes.add(nodes.label);
        }

        return setNodes;
    }

    // Obtém a VCD cujo valor do atributo primary é ON, ou seja, a VCD pai
    private String parentNode() {
        String rootNode = null;
        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
            if (elementsVCDS.getExtractList().get(i).getVCDMain() != null) {
                rootNode = elementsVCDS.getExtractList().get(i).getVCDMain();
            }
        }
        return rootNode;
    }
}
